package com.hope;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class EditNumberDialog extends DialogFragment {
	private DialogClickListener callback;
	private final String ARG_OBJECT_FIELD_NUM = "number_dialog";
	private final String ARG_OBJECT_FIELD = "number_dialog2";
	private final String ARG_OBJECT_TITLE = "number_dialog3";
	private int field;
    
	public interface DialogClickListener {
	    public void onDialogPositiveClick(String s, int i);
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            callback = (DialogClickListener) getTargetFragment();
        } catch (ClassCastException e) {
            throw new ClassCastException("Calling fragment must implement DialogClickListener interface");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        
        final View view = inflater.inflate(R.layout.dialog_number, null);
        Bundle args = getArguments();
        field = args.getInt(ARG_OBJECT_FIELD_NUM);
        System.out.println(field);
        EditText info = (EditText)view.findViewById(R.id.number_field);
        info.setText(args.getString(ARG_OBJECT_FIELD));
        
        builder.setView(view)
        	.setTitle(args.getString(ARG_OBJECT_TITLE))
	        .setPositiveButton("Save", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	                // Send the positive button event back to the host activity
	            	EditText info = (EditText) view.findViewById(R.id.number_field);
	                callback.onDialogPositiveClick(info.getText().toString(), field);
	            }
	        })
	        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	            	EditNumberDialog.this.getDialog().cancel();
	            }
	        });
        return builder.create();
    }
    
    public String getArgObjectFieldNum() {
    	return ARG_OBJECT_FIELD_NUM;
    }
    
    public String getArgObjectField() {
    	return ARG_OBJECT_FIELD;
    }
    
    public String getArgObjectTitle() {
    	return ARG_OBJECT_TITLE;
    }
}